﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ArchivingDataWebForms;
using Telerik.Web.UI;
using System.IO;
using System.Reflection;
using System.Web.Security;


namespace ArchivingDataWebForms.Helpers
{
    public class WebFormBase : page
    {
        public WebFormBase() : base()
        {
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (!this.Request.Url.ToString().ToLower().Contains("Pages/Login/Webformlogin.aspx".ToLower()))
                {
                    FormsAuthentication.RedirectToLoginPage();
                }
            }
            base.OnLoad(e);
        }

        protected void InitializeTelerikRequest(WebControl control, string attribute)
        {
            string script = "function DoTelerikRequest(ElementUniqueID)" +
                            "{" +
                            "$find('" + RadAjaxManager.GetCurrent(this.Page).ClientID + "').ajaxRequestWithTarget(ElementUniqueID, '');" +
                            "}";
            RadScriptManager.RegisterStartupScript(this, this.GetType(), "DoTelerikRequest", script, true);
            control.Attributes[attribute] = "DoTelerikRequest('" + control.UniqueID + "');";
        }

        protected void ShowNotification(string text, NotificationIconType notificationIconType)
        {
            if (this.Master != null)
            {
                RadNotification defaultNotification = null;
                if (notificationIconType == NotificationIconType.Error)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationErrorDefault");
                }
                else if (notificationIconType == NotificationIconType.Ok)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationOkDefault");
                }
                else if (notificationIconType == NotificationIconType.Warning)
                {
                    defaultNotification = (RadNotification)this.Master.FindControl("RadNotificationWarningDefault");
                }
                if (defaultNotification != null)
                {
                    defaultNotification.Show(text);
                }
            }
        }

        protected void ShowWindow(string url)
        {
            string script = string.Format("window.radopen('{0}', null);", url);
            RadScriptManager.RegisterStartupScript(this, this.GetType(), "OpenWindow", script, true);
        }

        protected void CloseWindow()
        {
            string script = string.Format("var oWnd = GetRadWindow();oWnd.close();");
            RadScriptManager.RegisterStartupScript(this, this.GetType(), "CloseWindow", script, true);
        }

        protected void DoPostPackInsteadAjax(WebControl control)
        {
            RadScriptManager.GetCurrent(this.Page).RegisterPostBackControl(control);
        }

        protected void Redirect(string url,int milleseconds)
        {
            RadScriptManager.RegisterStartupScript(this, this.GetType(), "Redirect", "Redirect('" + url + "'," + milleseconds + ");", true);
        }

    }
}